﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace FileRenamer
{
    public class Logger
    {
        private RichTextBox m_loggerText = new RichTextBox();
        private MessageType m_DisplayLevel = MessageType.INFORMATION;

        public Logger()
        {
        }

        public string GetLoggerText()
        {
            return m_loggerText.Rtf;
        }
         
        public void StartNewLogger()
        {
            m_loggerText = new RichTextBox();
        }

        public void SetLevel(MessageType displayLevel)
        {
            m_DisplayLevel = displayLevel;
        }

        /// <summary>
        /// Display log message in log output location using the supplied format. Level of message can be changed.
        /// </summary>
        /// <param name="text">Text to be displayed in log message</param>
        /// <param name="type">Type of message based on enum value</param>
        /// <param name="color">Colour for the text</param>
        /// <param name="newline">Is a new line required after string</param>
        public void Log(String text, MessageType type, Color color, bool newline)
        {
            if (type >= m_DisplayLevel)
            {
                SetLogColour(color);
                AddLogText(text);

                if (newline)
                {
                    AddLogText(Environment.NewLine);
                }
            }
        }

        private void SetLogColour(Color colour)
        {
            if (m_loggerText.InvokeRequired)
            {
                MethodInvoker del = delegate { SetLogColour(colour); };
                m_loggerText.Invoke(del);
            }
            else
            {
                m_loggerText.SelectionColor = colour;
            }
        }

        private void AddLogText(string text)
        {
            if (m_loggerText.InvokeRequired)
            {
                MethodInvoker del = delegate { AddLogText(text); };
                m_loggerText.Invoke(del);
            }
            else
            {
                m_loggerText.AppendText(text);
            }
        }
    }
}
